import {getMemberDataFields} from "@src/api/member";
import {storeBindingsBehavior} from "mobx-miniprogram-bindings";
import member from "@src/store/member";

Component({
  options: {
    addGlobalClass: true
  },
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: member,
    fields: ['wxName'],
  }],
  data: {
    packingArray: ['男','女'],
    showZZPDModal: false,
    cModalTitle: '',
    modalItemData: null,
    peronInfoData: null,
    modalInputValue: ''
  },
  lifetimes: {
    ready() {
      this.getCurrentData();
      this.memberDataFields();
    }
  },
  methods: {
    changInfoName(e) {
      let itemData = e.currentTarget.dataset.item
      let modalInputValue = this.data.peronInfoData[itemData.alias]
      this.setData({
        showZZPDModal: true,
        cModalTitle: itemData.fieldName,
        modalItemData: itemData,
        modalInputValue: modalInputValue
      })
    },
    cancelInfoModal() {
      this.setData({modalInputValue: "", showZZPDModal: false})
    },
    //modal点击确定(同步数据及渲染页面)
    confirmInfoModal() {
      //正则验证
      this.regValify();
      let peronInfoData = this.data.peronInfoData;
      peronInfoData[this.data.modalItemData.alias] = this.data.modalInputValue;
      this.setData({modalInputValue: "", showZZPDModal: false, peronInfoData: peronInfoData})
    },
    /*输入框正则验证*/
    regValify() {//
      this.sampleValify(this.data.modalInputValue, this.data.modalItemData)
    },
    sampleValify(val, itemData) {
      if (!(itemData.require)) {
        return false
      }
      if (val == null || val === "") {
        console.log('非空');
        wx.showToast({
          title: `${itemData.fieldName}字段不能为空`,
          icon: 'none',
          duration: 3000
        });
        return false;
      }
      if (itemData.alias === 'email' && !/^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/.test(val)) {
        wx.showToast({
          title: `${itemData.fieldName}字段输入有误`,
          icon: 'none',
          duration: 3000
        });
        return false;
      }
      if (itemData.alias === 'idcard' && !/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(val)) {
        wx.showToast({
          title: `${itemData.fieldName}字段输入有误`,
          icon: 'none',
          duration: 3000
        });
        return false;
      }
      return true;
    },
    //点击选择地址
    getLocationD() {
      wx.chooseLocation({
        success: (res) => {
          //更新页面数据
          let peronInfoData = this.data.peronInfoData
          peronInfoData.location = res.address;
          this.setData({peronInfoData: peronInfoData})
        },
        fail: (e) => {
          if (e.errMsg.indexOf("auth deny") !== -1) {
            wx.showModal({
              title: '位置授权提示',
              content: '由于您未授权请按以下步骤进行位置授权 \n 1.点击右上角（三个点的位置） - 2.选择关于 - 3.再点击右上角 - 4.选择设置- 5.开启相应权限开关',
              showCancel: false,
              success(res) {
                if (res.confirm) {
                }
              }
            })
          }
        }
      })
    },
    //性别选择
    bindPickerChange(e) {
      let peronInfoData = this.data.peronInfoData;
      if (e.detail.value === 1) {
        peronInfoData.sex = 0;
      } else {
        peronInfoData.sex = 1;
      }
      this.setData({peronInfoData: peronInfoData})
    },
    bindDateChangeStart(event) {
      let peronInfoData = this.data.peronInfoData;
      peronInfoData.birthday = event.detail.value;
      console.info("event.target.value", event)
      this.setData({peronInfoData: peronInfoData})
    },
    handleModalInput(e) {
      this.setData({
        modalInputValue: e.detail.value
      })
    },
    clearModelInput(e) {
      this.setData({
        modalInputValue: ""
      })
    },
    validItemArr() {
      let peronInfoData = JSON.parse(JSON.stringify(this.data.peronInfoData));
      this.data.isNeedInputItemArr.forEach(value => {
        if (!peronInfoData[value]) {
          wx.showToast({
            title: `${this.data.inputKV[value]}字段不能为空`,
            icon: 'none',
            duration: 3000
          });
        }
        return false;
      })
      Object.keys(peronInfoData).forEach(value => {
        if (!peronInfoData[value]) {
          delete peronInfoData[value];
        }
      })
      return peronInfoData;
    },
    memberDataFields() {
      getMemberDataFields().then(res => {
        if (res.code === 0) {//请求成功
          let reqData = res.data;
          reqData.sort((a, b) => b.require - a.require)
          let memberDataFieldsDt = reqData;
          let peronInfoData = {};
          let inputKV = {};
          reqData.forEach(value => {
            inputKV[value.alias] = value.fieldName;
          });

          //获取必填的字段
          let isNeedInputItemArr = reqData.map(value => {
            if (value) {
              //对象增加默认字段
              peronInfoData[value.alias] = "";
            }
            if (value.require && (value.alias !== "mobile")) {
              return value.alias
            }
          }).filter(value => {
            return value
          })
          peronInfoData.name = this.data.wxName;
          peronInfoData.sex = -1;
          this.setData({peronInfoData, memberDataFieldsDt, inputKV, isNeedInputItemArr})

        }
      })
    },
    //获取当前时间
    getCurrentData() {
      let myDate = new Date();
      let getYear = myDate.getFullYear();
      let Month = myDate.getMonth() + 1
      let getMonth = Month > 9 ? Month.toString : '0' + Month
      let getDay = myDate.getDay() > 9 ? myDate.getDay().toString : '0' + myDate.getDay()
      let startDate = `${getYear - 120}-${getMonth}-${getDay}`
      let endDate = `${getYear}-${getMonth}-${getDay}`
      let pickerStart = `${getYear - 20}-${getMonth}-${getDay}`
      this.setData({startDate, endDate, pickerStart})
    },
    handleStop() {
      return false
    },
  }
});
